<?php
	include('includes/config.php');	
	$file = $config['photodir'] . user_input('image');
	$path = explode('/',$file);
	$filename = array_pop($path);
	foreach($path as $value){
		$thumb = $thumb . $value . "/";
	}
	$thumb = $thumb . "." . $filename;
	$ext = array_pop(explode('.',$filename));
if (file_exists($thumb)){
	header('Location: ' . $thumb);
} else {
	list($width, $height) = getimagesize($file);
	if ($width > $height){
		$modwidth = $config['thumbw'];
		$modheight = ($config['thumbw'] / $width) * $height;
	}else{
		$modheight = $config['thumbh'];
		$modwidth = ($config['thumbh'] / $height) * $width;
	}
	$tn = imagecreatetruecolor($modwidth, $modheight);
	if(strtolower($ext)=="jpg" or strtolower($ext)=="jpeg"){

		$image = imagecreatefromjpeg($file);
		imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
		imagejpeg($tn, $thumb);
		header('Location: ' . $thumb);
		imagedestroy($tn);
	}else{
		$image = imagecreatefrompng($file);
		imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
		imagepng($tn, $thumb);
		header('Location: ' . $thumb);
		imagedestroy($tn);
	}
}
?>
